package com.earnings.okhttputils.utils.ui.fragment.transaction;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.https.CommonAccess;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.BaseFragment;
import com.earnings.okhttputils.utils.utils.CommonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 线下充值
 * Created by HOHO on 2017/4/11.
 */

public class RechargeOfflineFragment extends BaseFragment {


    public static  int nowStep;
    public static  final  int STEP_FIRST=1;
    public static  final  int STEP_SCONED=2;
    public static  final  int STEP_THIRD=3;
    public File imgFile;
    public String money;
   public RechargeOfflineStepThirdFragment thirdFragment;
    @Override
    protected void init() {
        thirdFragment=new RechargeOfflineStepThirdFragment();
        initFramgent(new RechargeOfflineStepFirstFragment(),new RechargeOfflineStepSecondFragment(),thirdFragment);

    }

    @Override
    protected int onCreateLayout() {
        return R.layout.fragment_recharge_offline;
    }








    private List<Fragment> fms;
    private int choose=1;
    /**
     * 初始化底部栏(逻辑代码无需看懂)
     */
    public void initFramgent(Fragment...fm){
        nowStep=STEP_FIRST;
        fms=new ArrayList<Fragment>();
        FragmentManager fmmanager=getActivity().getSupportFragmentManager();
        FragmentTransaction ft=fmmanager.beginTransaction();
        for (int i = 0; i < fm.length; i++) {
            if(fm[i]==null){fms.add(null);continue;}
            ft.add(R.id.main_framelayout, fm[i]);
            if(i==0)ft.show(fm[i]);
            else ft.hide(fm[i]);
            fms.add(fm[i]);

        }
        ft.commit();
        //choose();
    };


    @Override
    public void onClick(View v) {
    }

    public void runStep(int step){

        FragmentTransaction ft=getActivity().getSupportFragmentManager().beginTransaction();
        for (int i = 0; i <fms.size(); i++) {
            ft.hide(fms.get(i));
        }
        if(nowStep>step)ft.setCustomAnimations(R.anim.right_in,R.anim.left_out);
        else if(nowStep<step)ft.setCustomAnimations(R.anim.left_in, R.anim.right_out);

        nowStep=step;
        ft.show(fms.get(step-1));
        ft.commit();
        initBtn();

    }

    public void initBtn(){
        for (int i = 1; i <=3 ; i++) {
            int tvid=getIdFromXML("text"+i,mView);
            int iconid=getIdFromXML("icon"+i,mView);
            int lineid=getIdFromXML("line"+i,mView);
            ImageView icon= (ImageView) mView.findViewById(iconid);
            View line=mView.findViewById(lineid);
            TextView text= (TextView) mView.findViewById(tvid);
            if(nowStep<i){
                line.setBackgroundColor(Color.parseColor("#cccccc"));
                text.setTextColor(Color.parseColor("#cccccc"));
                if(i==STEP_SCONED)icon.setImageResource(R.mipmap.offline_icon10);
                if(i==STEP_THIRD)icon.setImageResource(R.mipmap.offline_icon20);
            }else{

                line.setBackgroundColor(Color.parseColor("#f88465"));
                text.setTextColor(Color.parseColor("#f88465"));
                if(i==STEP_SCONED)icon.setImageResource(R.mipmap.offline_icon11);
                if(i==STEP_THIRD)icon.setImageResource(R.mipmap.offline_icon21);
            }
        }

    }

    public void imgUpload(final String money, final String phone){
        showProgress();
        this.money=money;
        CommonAccess<String> common=new CommonAccess<>( getActivity() );
        common.setCallbacks( new HttpArrayCallback<String>(getActivity()) {
            @Override
            public void onSuccess(List<String> object, String msg) {
                try {
                    JSONObject imgobject=new JSONObject( object.get( 0 ) );
                    rechargeOfflineSumbit(imgobject.getString( "urlpath" ),phone);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFail(int code, String msg) {
                showToast( msg );
            }
        } );
        common.uploadImg( imgFile );
    }
          /*
   提交信息
 */


    public void rechargeOfflineSumbit(String imgurl,String phone){

            Map map = new HttpMap( getActivity() );

            map.put( "account",phone);
            map.put( "money", money );
             map.put( "img", imgurl );

        OkHttpUtils.post().url( HttpUrl.USER_RECHARGE_OFFLINE_SUMBIT_URL ).params( map ).build().execute( new HttpObjectCallback<String>(  getContext() ) {
                @Override
                public void onSuccess(String object, String msg) {
dismissProgress();
                    runStep( STEP_THIRD );
                    thirdFragment.initData( object );
                    showToast( msg );
                }

                @Override
                public void onFail(int code, String msg) {
                    dismissProgress();
                        showToast( msg );
                }


            } );

    }
}
